package http;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Map.Entry;

public class RapidShareCookie {
	final private String urlstring;
	final private String data;
	public RapidShareCookie(String name,String password) {
		data = "uselandingpage=1&login="+name+"&password="+password;
		urlstring = "https://ssl.rapidshare.com/cgi-bin/premiumzone.cgi";
	}
	public String getCookie() {
		String encCookie = null;
		try {
			System.out.println("Opening POST-Connection to "+urlstring);
			URL postUrl = new URL(urlstring);
			URLConnection urlc;
			urlc = postUrl.openConnection();
			urlc.setDoOutput(true);
	        OutputStreamWriter wr = new OutputStreamWriter(urlc.getOutputStream());
	        wr.write(data);
	        wr.flush();
			System.out.println("POST-Data: "+data);

	        for(Entry<String, List<String>> values:urlc.getHeaderFields().entrySet())
				for (String entry:values.getValue())
					System.out.println(values.getKey()+": "+entry);

	        StringTokenizer cookieData = new StringTokenizer(urlc.getHeaderField("Set-Cookie"),";");
	        encCookie = (String) cookieData.nextElement();
		} catch (IOException e) {}
        return encCookie;
	}
}